package paper.web.controller.topic;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import keter.domain.validation.BeanValidators;
import keter.web.util.MediaTypes;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import paper.dao.CommentsDao;
import paper.domain.Comments;
import paper.domain.Topic;
import paper.service.TopicService;
import paper.service.UserService;
import paper.util.ConfigConstant;
import paper.web.controller.BaseController;

@Controller
@PreAuthorize("hasRole('TEACHER')")
@RequestMapping("/paper/topic/mytopic")
public class TeacherTopicController extends BaseController {
	
	/**
	 * Logger for this class
	 */
	private static final Logger logger = LoggerFactory.getLogger(TeacherTopicController.class);

	@Autowired
	TopicService service;
	
	@Autowired
	UserService stuService;
	
	@Autowired
	CommentsDao cdao;

	/* 系统配置常量 */
	@Autowired
	public ConfigConstant conf;

	public static class Views {
		private static final String Prefix = "paper/topic/mytopic";
		static final String Index = to_(Prefix) + "index";
		static final String Add = to_(Prefix) + "add";
		static final String Update = to_(Prefix) + "update";
		static final String View = to_(Prefix) + "view";
		static final String Delete = to_(Prefix) + "delete";
	}

	/**
	 * <p>
	 * Method ：index
	 * <p>
	 * Description : 进入列表页面
	 * 
	 * @param model
	 * @return
	 * @author gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = Routes.Index, method = RequestMethod.GET)
	public String index(Model model) {
		return Views.Index;
	}

	/**
	 * <p>
	 * Method ：getMyTopic
	 * <p>
	 * Description : 获取列表数据
	 * 
	 * @return
	 * @throws Exception
	 * @author 顾力行-gulixing@msn.com
	 */
	@RequestMapping(value = Routes.GetAll, method = RequestMethod.GET)
	@ResponseBody
	public List<Topic> getAll() throws Exception {
		return service.getTopicDao().findByCreator(getCurrentUser().getId());
	}

	@RequestMapping(value = Routes.Add, method = RequestMethod.GET)
	public String add() {
		return Views.Add;
	}

	/**
	 * <p>
	 * Method ：add
	 * <p>
	 * Description : 从前台接收内容转换成Account对象
	 * 然后调用AccountService进行持久化
	 * 
	 * @param model
	 * @return
	 * @author 顾力行-gulixing@msn.com
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value = Routes.Add, method = RequestMethod.POST, consumes = MediaTypes.JSON)
	@ResponseBody
	public Map<String, String> add(@RequestBody Topic topic, HttpServletResponse response) throws UnsupportedEncodingException {
    	Map<String, String> ajaxMap = getAjaxMap();
		topic.setCreatorId(getCurrentUser().getId());
		BeanValidators.validateWithException(validator, topic);
		service.save(topic);
		return ajaxMap;
	}

	/**
	 * <p>Method ：view
	 * <p>Description : 进入查看页面
	 * @param id
	 * @param model
	 * @return 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = Routes.View, method = RequestMethod.GET)
	public String view(@PathVariable Long id, Model model) {
		model.addAttribute("topic", service.getTopicDao().findOne(id));
		return Views.View;
	}
	
	/**
	 * <p>
	 * Method ：update
	 * <p>
	 * Description : 进入修改页面
	 * 
	 * @return
	 * @author gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = Routes.Update, method = RequestMethod.GET)
	public String update(@PathVariable Long id, Model model) {
		model.addAttribute("topic", service.getTopicDao().findOne(id));
		return Views.Update;
	}

	/**
	 * <p>
	 * Method ：getOne
	 * <p>
	 * Description : 读取修改对象数据
	 * 
	 * @param id
	 * @return
	 * @author gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = Routes.Get, method = RequestMethod.GET, consumes = MediaTypes.JSON)
	@ResponseBody
	public Topic getOne(@PathVariable Long id) {
		return service.getTopicDao().findOne(id);
	}

	/**
	 * <p>Method ：update
	 * <p>Description : 修改数据
	 * @param topic
	 * @param response
	 * @return 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = Routes.Update, method = RequestMethod.POST, consumes = MediaTypes.JSON)
	@ResponseBody
	public Map<String, String> update(@RequestBody Topic topic) {
    	Map<String, String> ajaxMap = getAjaxMap();
		topic.setCreatorId(getCurrentUser().getId());
		BeanValidators.validateWithException(validator, topic);
		service.save(topic);
		return ajaxMap;
	}

	
	@RequestMapping(value = Routes.Delete, method = RequestMethod.GET, consumes = MediaTypes.JSON)
	@ResponseBody
	public Map<String, String> delete(@PathVariable Long id) {
		Map<String, String> ajaxMap = getAjaxMap();
		service.deleteMyTopic(id,getCurrentAccount().getId());
		return ajaxMap;
	}
	
	@RequestMapping(value = "/viewcomments/{topicId}", method = RequestMethod.GET)
	@ResponseBody
	public List<Comments> comments(@PathVariable Long topicId) throws UnsupportedEncodingException {
    	return cdao.findByMainId(topicId);
	}
}